﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;

namespace Runda.CMS.Portal.ContractFollowUp
{
    public partial class InstrumentConservationExtra : BasePage
    {
        private SysDictionaryBll sysBll = new SysDictionaryBll();

        public Guid MaintainID
        {
            get
            {
                if (hiddenMaintainID.Value == string.Empty) return Guid.Empty;
                else return new Guid(hiddenMaintainID.Value);
            }
            set
            {
                hiddenMaintainID.Value = value.ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                initForm();
            }
        }

        private void initForm()
        {
            if (Request.QueryString["MaintainID"] != null && Request.QueryString["MaintainID"] != "")
            {
                MaintainID = new Guid(Request.QueryString["MaintainID"]);
                WsmMaintainBillEntity entity = new SystemBll().Get<WsmMaintainBillEntity>(new Guid(Request.QueryString["MaintainID"]));
                WsmTaskRegisterEntity taskEntity = new SystemBll().Get<WsmTaskRegisterEntity>(entity.RegisterID);
                if (taskEntity.TaskStatus == 1) {
                    lnkbtnSaveAndClose.Visible = false;
                }
                List<WsmConservationEntity> conservationEntitys = new SystemBll().GetAll<WsmConservationEntity>(" and ConservationID in (select ConservationID from Cms_Wsm_ConservationInInstrument where InstrumentSeries = '"+taskEntity.InstrumentID+"')");
                foreach (WsmConservationEntity conservationEntity in conservationEntitys) { 
                    Conservations.Items.Add(new ListItem(conservationEntity.ConservationName,conservationEntity.ConservationID.ToString()));
                }
                List<WsmConservationContentEntity> sysDicEntitys = sysBll.GetAll<WsmConservationContentEntity>(" and MaintainID = '" + Request.QueryString["MaintainID"] + "'");
                for(int index = 0; index< Conservations.Items.Count;index++){
                    for (int i = 0; i < sysDicEntitys.Count; i++) {
                        if (sysDicEntitys[i].ConservationID.ToString() == Conservations.Items[index].Value) {
                            Conservations.Items[index].Selected = true;
                        }
                    }
                }
            }
        }

        private void save()
        {
            List<SqlDbParameter> list = new List<SqlDbParameter>();
            SqlDbParameter parm = new SqlDbParameter();
            parm.ColumnName = "MaintainID";
            parm.ColumnType = DbType.String;
            parm.ParameterName = "@MaintainID";
            parm.ParameterValue = MaintainID.ToString();
            parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
            list.Add(parm);
            sysBll.LogicDelete<WsmConservationContentEntity>(list);

            for (int index = 0; index < Conservations.Items.Count; index++) {
                if (Conservations.Items[index].Selected) {
                    WsmConservationContentEntity entity = new WsmConservationContentEntity();
                    entity.ConservationID = new Guid(Conservations.Items[index].Value);
                    entity.MaintainID = MaintainID;
                    entity.ContentID = Guid.NewGuid();
                    sysBll.Insert(entity);
                }
            }
        }
        protected void btnchkAll_Onclick(object sender, EventArgs e)
        {
            foreach (ListItem li in Conservations.Items) {
                li.Selected = true;
            }
        }
        protected void btnUnchkAll_Onclick(object sender, EventArgs e) {
            foreach (ListItem li in Conservations.Items)
            {
                li.Selected = false;
            }
        }

        protected Boolean checkData()
        {
            Boolean rtnValue = true;
            int selCount = 0;
            for (int index = 0; index < Conservations.Items.Count; index++)
            {
                if (Conservations.Items[index].Selected)
                {
                    selCount++;
                }
            }
            if (selCount < 2) { rtnValue = false; }
            return rtnValue;
        }
        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            if (!checkData())
            { 
                ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('请选择保养项目');window.close();", true);
                return;
            }
            save();
            ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功');window.close();", true);
        }
    }
}
